.\"
.\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for
.\" permission to reproduce portions of its copyrighted documentation.
.\" Original documentation from The Open Group can be obtained online at
.\" http://www.opengroup.org/bookstore/.
.\"
.\" The Institute of Electrical and Electronics Engineers and The Open
.\" Group, have given us permission to reprint portions of their
.\" documentation.
.\"
.\" In the following statement, the phrase ``this text'' refers to portions
.\" of the system documentation.
.\"
.\" Portions of this text are reprinted and reproduced in electronic form
.\" in the SunOS Reference Manual, from IEEE Std 1003.1, 2004 Edition,
.\" Standard for Information Technology -- Portable Operating System
.\" Interface (POSIX), The Open Group Base Specifications Issue 6,
.\" Copyright (C) 2001-2004 by the Institute of Electrical and Electronics
.\" Engineers, Inc and The Open Group.  In the event of any discrepancy
.\" between these versions and the original IEEE and The Open Group
.\" Standard, the original IEEE and The Open Group Standard is the referee
.\" document.  The original Standard can be obtained online at
.\" http://www.opengroup.org/unix/online.html.
.\"
.\" This notice shall appear on any product containing this material.
.\"
.\" The contents of this file are subject to the terms of the
.\" Common Development and Distribution License (the "License").
.\" You may not use this file except in compliance with the License.
.\"
.\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
.\" or http://www.opensolaris.org/os/licensing.
.\" See the License for the specific language governing permissions
.\" and limitations under the License.
.\"
.\" When distributing Covered Code, include this CDDL HEADER in each
.\" file and include the License file at usr/src/OPENSOLARIS.LICENSE.
.\" If applicable, add the following below this CDDL HEADER, with the
.\" fields enclosed by brackets "[]" replaced with your own identifying
.\" information: Portions Copyright [yyyy] [name of copyright owner]
.\"
.\"
.\" Copyright 1989 AT&T
.\" Copyright 1992 X/Open Company Limited
.\" Portions Copyright (c) 1982-2007 AT&T Knowledge Ventures
.\" Copyright (c) 2008, Sun Microsystems, Inc. All Rights Reserved
.\"
.TH WAIT 1 "May 17, 2020"
.SH NAME
wait \- await process completion
.SH SYNOPSIS
.nf

.fi

.SS "/bin/sh"
.nf
\fBwait\fR [\fIpid\fR]...
.fi

.SS "/bin/jsh /bin/ksh /usr/xpg4/bin/sh"
.nf
\fBwait\fR [\fIpid\fR]...
.fi

.LP
.nf
\fBwait\fR [% \fIjobid\fR...]
.fi

.SS "/bin/csh"
.nf
\fBwait\fR
.fi

.SS "ksh93"
.nf
\fBwait\fR [\fIjob...\fR]
.fi

.SH DESCRIPTION
The shell itself executes \fBwait\fR, without creating a new process. If you
get the error message \fBcannot fork,too many processes\fR, try using the
\fBwait\fR command to clean up your background processes. If this doesn't help,
the system process table is probably full or you have too many active
foreground processes. There is a limit to the number of process \fBID\fRs
associated with your login, and to the number the system can keep track of.
.sp
.LP
Not all the processes of a pipeline with three or more stages are children of
the shell, and thus cannot be waited for.
.SS "/bin/sh, /bin/jsh"
Wait for your background process whose process \fBID\fR is \fIpid\fR and report
its termination status. If \fIpid\fR is omitted, all your shell's currently
active background processes are waited for and the return code is \fB0\fR. The
\fBwait\fR utility accepts a job identifier, when Job Control is enabled (jsh),
and the argument, \fIjobid\fR, is preceded by a percent sign (\fB%\fR).
.sp
.LP
If \fIpid\fR is not an active process \fBID,\fR the \fBwait\fR utility returns
immediately and the return code is \fB0\fR.
.SS "csh"
Wait for your background processes.
.SS "ksh"
When an asynchronous list is started by the shell, the process \fBID\fR of the
last command in each element of the asynchronous list becomes known in the
current shell execution environment.
.sp
.LP
If the \fBwait\fR utility is invoked with no operands, it waits until all
process \fBIDs\fR known to the invoking shell have terminated and exit with an
exit status of \fB0\fR.
.sp
.LP
If one or more \fIpid\fR or \fIjobid\fR operands are specified that represent
known process \fBID\fRs (or jobids), the \fBwait\fR utility waits until all of
them have terminated. If one or more \fIpid\fR or \fIjobid\fR operands are
specified that represent unknown process \fBID\fRs (or jobids), \fBwait\fR
treats them as if they were known process \fBID\fRs (or jobids) that exited
with exit status \fB127\fR. The exit status returned by the \fBwait\fR utility
is the exit status of the process requested by the last \fIpid\fR or
\fIjobid\fR operand.
.sp
.LP
The known process \fBID\fRs are applicable only for invocations of \fBwait\fR
in the current shell execution environment.
.SS "ksh93"
wait with no operands, waits until all jobs known to the invoking shell have
terminated. If one or more job operands are specified, wait waits until all of
them have completed. Each job can be specified as one of the following:
.sp
.ne 2
.na
\fB\fInumber\fR\fR
.ad
.RS 12n
\fInumber\fR refers to a process ID.
.RE

.sp
.ne 2
.na
\fB\fB-\fR\fInumber\fR\fR
.ad
.RS 12n
\fInumber\fR refers to a process group ID.
.RE

.sp
.ne 2
.na
\fB\fB%\fR\fInumber\fR\fR
.ad
.RS 12n
\fInumber\fR refers to a job number
.RE

.sp
.ne 2
.na
\fB\fB%\fR\fIstring\fR\fR
.ad
.RS 12n
Refers to a job whose name begins with \fIstring\fR
.RE

.sp
.ne 2
.na
\fB\fB%?\fR\fIstring\fR\fR
.ad
.RS 12n
Refers to a job whose name contains \fIstring\fR
.RE

.sp
.ne 2
.na
\fB\fB%+\fR\fR
.ad
.br
.na
\fB\fB%%\fR\fR
.ad
.RS 12n
Refers to the current job
.RE

.sp
.ne 2
.na
\fB\fB%-\fR\fR
.ad
.RS 12n
Refers to the previous job
.RE

.sp
.LP
If one or more job operands is a process id or process group id not known by
the current shell environment, \fBwait\fR treats each of them as if it were a
process that exited with status 127.
.SH OPERANDS
The following operands are supported:
.sp
.ne 2
.na
\fB\fIpid\fR\fR
.ad
.RS 9n
The unsigned decimal integer process \fBID\fR of a command, for which the
utility is to wait for the termination.
.RE

.sp
.ne 2
.na
\fB\fIjobid\fR\fR
.ad
.RS 9n
A job control job \fBID\fR that identifies a background process group to be
waited for. The job control job \fBID\fR notation is applicable only for
invocations of \fBwait\fR in the current shell execution environment, and only
on systems supporting the job control option.
.RE

.SH USAGE
On most implementations, \fBwait\fR is a shell built-in. If it is called in a
subshell or separate utility execution environment, such as one of the
following,
.sp
.in +2
.nf
(wait)
nohup wait ...
find . -exec wait ... \e;
.fi
.in -2
.sp

.sp
.LP
it returns immediately because there is no known process \fBID\fRs to wait for
in those environments.
.SH EXAMPLES
\fBExample 1 \fRUsing A Script To Identify The Termination Signal
.sp
.LP
Although the exact value used when a process is terminated by a signal is
unspecified, if it is known that a signal terminated a process, a script can
still reliably figure out which signal is using \fBkill\fR, as shown by the
following (\fB/bin/ksh\fR and \fB/usr/xpg4/bin/sh\fR):

.sp
.in +2
.nf
sleep 1000&
pid=$!
kill -kill $pid
wait $pid
echo $pid was terminated by a SIG$(kill -l $(($?\(mi128))) signal.
.fi
.in -2
.sp

.LP
\fBExample 2 \fRReturning The Exit Status Of A Process
.sp
.LP
If the following sequence of commands is run in less than 31 seconds
(\fB/bin/ksh\fR and \fB/usr/xpg4/bin/sh\fR):

.sp
.in +2
.nf
sleep 257 | sleep 31 &

jobs -l %%
.fi
.in -2
.sp

.sp
.LP
then either of the following commands returns the exit status of the second
\fBsleep\fR in the pipeline:

.sp
.in +2
.nf
wait <\fIpid of sleep 31\fR>
wait %%
.fi
.in -2
.sp

.SH ENVIRONMENT VARIABLES
See \fBenviron\fR(7) for descriptions of the following environment variables
that affect the execution of \fBwait\fR: \fBLANG\fR, \fBLC_ALL\fR,
\fBLC_CTYPE\fR, \fBLC_MESSAGES\fR, and \fBNLSPATH\fR.
.SH EXIT STATUS
.SS "ksh93"
The following exit values are returned by the \fBwait\fR built-in in
\fBksh93\fR:
.sp
.ne 2
.na
\fB\fB0\fR\fR
.ad
.RS 7n
\fBwait\fR was invoked with no operands. All processes known by the invoking
process have terminated.
.RE

.sp
.ne 2
.na
\fB\fB127\fR\fR
.ad
.RS 7n
\fIjob\fR is a process id or process group id that is unknown to the current
shell environment.
.RE

.SH ATTRIBUTES
See \fBattributes\fR(7) for descriptions of the following attributes:
.sp

.sp
.TS
box;
c | c
l | l .
ATTRIBUTE TYPE	ATTRIBUTE VALUE
_
Interface Stability	Committed
_
Standard	See \fBstandards\fR(7).
.TE

.SH SEE ALSO
.BR csh (1),
.BR jobs (1),
.BR ksh (1),
.BR ksh93 (1),
.BR sh (1),
.BR attributes (7),
.BR environ (7),
.BR standards (7)
